package cz.data.common.support.db;

import cz.data.common.exception.DataException;
import lombok.Data;
import lombok.NoArgsConstructor;
import org.apache.commons.lang3.StringUtils;

import java.io.Serializable;
import java.util.Objects;

@Data
@NoArgsConstructor
public class DbProperty implements Serializable {

    private static final long serialVersionUID = 1L;

    private String id;
    private String dbType;
    private String host;
    private String username;
    private String password;
    private Integer port;
    private String dbName;
    private String sid;
    private String schemaName;
    private int maxConn = 5;
    private DbType dbTypeEnum;

    public DbProperty(String id, String dbType, String host, String username, String password,
                      Integer port, String dbName, String sid, String schemaName, Integer maxConn) {
        this.id = id;
        this.dbType = dbType;
        this.host = host;
        this.username = username;
        this.password = password;
        this.port = port;
        this.dbName = dbName;
        this.sid = sid;
        this.schemaName = schemaName;
        this.dbTypeEnum = DbType.getDbType(dbType);
        if (maxConn != null) {
            this.maxConn = maxConn;
        }
    }

    /**
     * 参数合法性校验
     */
    public void valid() {
        if (StringUtils.isEmpty(dbType) || StringUtils.isEmpty(host) ||
                StringUtils.isEmpty(username) || StringUtils.isEmpty(password) ||
                Objects.isNull(port)) {
            throw new DataException("参数不完整");
        }
        if (DbType.OTHER.getDb().equals(dbType)) {
            throw new UnsupportedOperationException("不支持的数据库类型");
        }
    }
}
